home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kopete / kopetecommandhandler.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  7.7 KB  |  220 lines

  1. /*
  2.     kopetecommandhandler.h - Command Handler
  3.  
  4.     Copyright (c) 2003      by Jason Keirstead       <jason@keirstead.org>
  5.     Kopete    (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>
  6.  
  7.     *************************************************************************
  8.     *                                                                       *
  9.     * This library is free software; you can redistribute it and/or         *
  10.     * modify it under the terms of the GNU Lesser General Public            *
  11.     * License as published by the Free Software Foundation; either          *
  12.     * version 2 of the License, or (at your option) any later version.      *
  13.     *                                                                       *
  14.     *************************************************************************
  15. */
  16.  
  17. #ifndef _KOPETECOMMANDHANDLER_H_
  18. #define _KOPETECOMMANDHANDLER_H_
  19.  
  20. #include <qdict.h>
  21. #include <kshortcut.h>
  22. #include "kopetemessage.h"
  23.  
  24. #include "kopete_export.h"
  25.  
  26. class KProcess;
  27.  
  28. struct CommandHandlerPrivate;
  29.  
  30. class KopeteView;
  31. class KopeteCommandGUIClient;
  32.  
  33. namespace Kopete
  34. {
  35.  
  36. class ChatSession;
  37. class Plugin;
  38. class Protocol;
  39. class Command;
  40.  
  41. typedef QDict<Command> CommandList;
  42.  
  43. /**
  44.  * @author Jason Keirstead   <jason@keirstead.org>
  45.  *
  46.  * The Kopete::CommandHandler can handle /action like messages
  47.  */
  48. class KOPETE_EXPORT CommandHandler : public QObject
  49. {
  50.     friend class ::KopeteCommandGUIClient;
  51.  
  52.     Q_OBJECT
  53.  
  54.     public:
  55.         /**
  56.          * an enum defining the type of a command
  57.          */
  58.         enum CommandType { Normal, SystemAlias, UserAlias, Undefined };
  59.  
  60.         /**
  61.          * Returns a pointer to the command handler
  62.          */
  63.         static CommandHandler *commandHandler();
  64.  
  65.         /**
  66.          * \brief Register a command with the command handler.
  67.          *
  68.          * Command matching is case insensitive. All commands are registered,
  69.          * regardless of whether  or not they are already handled by another
  70.          * handler. This is so that if the first plugin is unloaded, the next
  71.          * handler in the sequence will handle the command. However, there are
  72.          * certain commands which are reserved (internally handled by the
  73.          * Kopete::CommandHandler). These commands can also be overridden by
  74.          * registering a new duplicate command.
  75.          *
  76.          * @param parent The plugin who owns this command
  77.          * @param command The command we want to handle, not including the '/'
  78.          * @param handlerSlot The slot used to handle the command. This slot must
  79.          *   accept two parameters, a QString of arguments, and a Kopete::ChatSession
  80.          *   pointer to the manager under which the command was sent.
  81.          * @param help An optional help string to be shown when the user uses
  82.          *   /help \<command\>
  83.          * @param minArgs the minimum number of arguments for this command
  84.          * @param maxArgs the maximum number of arguments this command takes
  85.          * @param cut a default keyboard shortcut
  86.          * @param pix icon name, the icon will be shown in menus
  87.          */
  88.         void registerCommand( QObject *parent, const QString &command, const char* handlerSlot,
  89.             const QString &help = QString::null, uint minArgs = 0, int maxArgs = -1,
  90.             const KShortcut &cut = 0, const QString &pix = QString::null );
  91.  
  92.         /**
  93.          * \brief Register a command alias.
  94.          *
  95.          * @param parent The plugin who owns this alias
  96.          * @param alias The command for the alias
  97.          * @param formatString This is the string that will be transformed into another
  98.          *    command. The formatString should begin with an already existing command,
  99.          *    followed by any other arguments. The variables %1, %2... %9 will be substituted
  100.          *    with the arguments passed into the alias. The variable %s will be substituted with
  101.          *    the entire argument string
  102.          * @param help An optional help string to be shown when the user uses
  103.          *   /help \<command\>
  104.          * @param minArgs the minimum number of arguments for this command
  105.          * @param maxArgs the maximum number of arguments this command takes
  106.          * @param cut a default keyboard shortcut
  107.          * @param pix icon name, the icon will be shown in menus
  108.          */
  109.         void registerAlias( QObject *parent,
  110.             const QString &alias,
  111.             const QString &formatString,
  112.             const QString &help = QString::null,
  113.             CommandType = SystemAlias,
  114.             uint minArgs = 0,
  115.             int maxArgs = -1,
  116.             const KShortcut &cut = 0,
  117.             const QString &pix = QString::null );
  118.  
  119.         /**
  120.          * \brief Unregister a command.
  121.          *
  122.          * When a plugin unloads, all commands are automaticlly unregistered and deleted.
  123.          * This function should only be called in the case of a plugin which loads and
  124.          * unloads commands dynamically.
  125.          *
  126.          * @param parent The plugin who owns this command
  127.          * @param command The command to unload
  128.          */
  129.         void unregisterCommand( QObject *parent, const QString &command );
  130.  
  131.         /**
  132.          * \brief Unregister an alias.
  133.          *
  134.          * \see unregisterCommand( QObject *parent, const QString &command )
  135.          * @param parent The plugin who owns this alias
  136.          * @param alias The alais to unload
  137.          */
  138.         void unregisterAlias( QObject *parent, const QString &alias );
  139.  
  140.         /**
  141.          * \brief Process a message to see if any commands should be handled
  142.          *
  143.          * @param msg The message to process
  144.          * @param manager The manager who owns this message
  145.          * @return True if the command was handled, false if not
  146.          */
  147.         bool processMessage( Message &msg, ChatSession *manager );
  148.  
  149.         /**
  150.          * \brief Process a message to see if any commands should be handled
  151.          *
  152.          * \see processMessage( Kopete::Message &msg, Kopete::ChatSession *manager)
  153.          * \param msg A QString contain the message
  154.          * \param manager the Kopete::ChatSession who will own the message
  155.          * \return true if the command was handled, false if the command was not handled.
  156.          */
  157.         bool processMessage( const QString &msg, ChatSession *manager );
  158.  
  159.         /**
  160.          * Parses a string of command arguments into a QStringList. Quoted
  161.          * blocks within the arguments string are treated as one argument.
  162.          */
  163.         static QStringList parseArguments( const QString &args );
  164.  
  165.         /**
  166.          * \brief Check if a command is already handled
  167.          *
  168.          * @param command The command to check
  169.          * @return True if the command is already being handled, False if not
  170.          */
  171.         bool commandHandled( const QString &command );
  172.  
  173.         /**
  174.          * \brief Check if a command is already handled by a spesific protocol
  175.          *
  176.          * @param command The command to check
  177.          * @param protocol The protocol to check
  178.          * @return True if the command is already being handled, False if not
  179.          */
  180.         bool commandHandledByProtocol( const QString &command, Protocol *protocol);
  181.  
  182.     private slots:
  183.         void slotPluginLoaded( Kopete::Plugin * );
  184.         void slotPluginDestroyed( QObject * );
  185.         void slotExecReturnedData(KProcess *proc, char *buff, int bufflen );
  186.         void slotExecFinished(KProcess *proc);
  187.         void slotViewCreated( KopeteView *view );
  188.  
  189.         void slotHelpCommand( const QString & args, Kopete::ChatSession *manager );
  190.         void slotClearCommand( const QString & args, Kopete::ChatSession *manager );
  191.         void slotPartCommand( const QString & args, Kopete::ChatSession *manager );
  192.         void slotCloseCommand( const QString & args, Kopete::ChatSession *manager );
  193.         //void slotMeCommand( const QString & args, Kopete::ChatSession *manager );
  194.         void slotExecCommand( const QString & args, Kopete::ChatSession *manager );
  195.         void slotAwayCommand( const QString & args, Kopete::ChatSession *manager );
  196.         void slotAwayAllCommand( const QString & args, Kopete::ChatSession *manager );
  197.         void slotSayCommand( const QString & args, Kopete::ChatSession *manager );
  198.  
  199.     private:
  200.         /**
  201.          * Helper function. Returns all the commands that can be used by a KMM of this protocol
  202.          * (all non-protocol commands, plus this protocols commands)
  203.          */
  204.         CommandList commands( Protocol * );
  205.  
  206.         /**
  207.          * Helper function for commands()
  208.          */
  209.         void addCommands( CommandList &from, CommandList &to, CommandType type = Undefined );
  210.  
  211.         CommandHandler();
  212.         ~CommandHandler();
  213.  
  214.         static CommandHandlerPrivate *p;
  215. };
  216.  
  217. }
  218.  
  219. #endif
  220.